package com.shift.converter;

import java.net.URL;

import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;

/**
 * The activator class controls the plug-in life cycle
 */
public class Activator extends AbstractUIPlugin {

	// The plug-in ID
	public static final String PLUGIN_ID = "com.shift.converter.wizard";
	
	// The shared instance
	private static Activator plugin;

	private static final String PATH_ICONS = "icons/";
	
	public static final String IMG_NEW_WIZARD = "IMG_NEW_WIZARD";	
	public static final String IMG_NEW_WIZARD_ULR = "new_wizard.gif";
	//public static final String IMG_TREE_COL_ULR = "data_column.gif";
	//public static final String IMG_TREE_TABLE_URL = "table.gif";
	
	/**
	 * The constructor
	 */
	public Activator() {
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)
	 */
	public void start(BundleContext context) throws Exception {
		super.start(context);
		plugin = this;
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
	 */
	public void stop(BundleContext context) throws Exception {
		plugin = null;
		super.stop(context);
	}

	/**
	 * Returns the shared instance
	 *
	 * @return the shared instance
	 */
	public static Activator getDefault() {
		return plugin;
	}

	private void registerImage(ImageRegistry registry, String key, String fileName) {
		try {
			IPath path = new Path(PATH_ICONS + fileName);
			URL url = FileLocator.find(getDefault().getBundle(), path, null);
			if(url != null) {
				ImageDescriptor desc = ImageDescriptor.createFromURL(url);
				registry.put(key, desc);
			}
		} catch(Exception e) {
		}
	}
	
	public Image getImage(String key) {
		return getImageRegistry().get(key);
	}
	
	protected void initializeImageRegistry(ImageRegistry registry) {
		registerImage(registry, IMG_NEW_WIZARD, IMG_NEW_WIZARD_ULR);
	}
	
	public static ImageDescriptor getImageDescriptor(String path) {
		return imageDescriptorFromPlugin(PLUGIN_ID, path);
	}
	
	public static void log(IStatus status) {
		getDefault().getLog().log(status);
	}
	
	public static void log(Throwable e) {
		log(new Status(IStatus.ERROR, getPluginId(), IStatus.ERROR, "Error", e));
	}
	
	public static String getPluginId() {
		return getDefault().getBundle().getSymbolicName(); 
	}
	
	public static IWorkbenchPage getActivePage() {
		IWorkbenchWindow activeWrokbenchWindow = getActiveWorkbenchWindow();
		if(activeWrokbenchWindow == null)
			return null;
		return activeWrokbenchWindow.getActivePage();
	}
	
	public static IWorkbenchWindow getActiveWorkbenchWindow() {
		if(plugin == null)
			return null;
		IWorkbench workBench = plugin.getWorkbench();
		if(workBench == null)
			return null;
		return workBench.getActiveWorkbenchWindow();
	}
}
